﻿# -*- coding:utf-8 -*-

"""
This is the basic interface to fetch http content.
"""

import httplib

class snHTTPDownloader(object):
	def __init__(self, host, path, port=80):
		super(snHTTPDownloader, self).__init__()
		self._host = host
		self._path = path
		self._port = port

	def GetContent(self):
		conn = httplib.HTTPConnection(self._host, self._port)
		conn.request("GET", self._path)
		resp = conn.getresponse()
		if resp.status == httplib.OK:
			data = resp.read()
			return data
		else:
			return None

def test():
	hd = snHTTPDownloader("www.baidu.com", "/s?ie=utf-8&bs=%E6%BB%9D%E6%B2%A2%E7%BE%8E%E9%93%83&f=8&rsv_bp=1&rsv_spt=3&wd=%E8%8F%85%E9%87%8E%E4%BA%9A%E6%A2%A8%E6%B2%99&rsv_n=2&rsv_sug3=1&rsv_sug=0&rsv_sug1=1&rsv_sug4=426&inputT=1168")
	data = hd.GetContent()
	print data
	fw = open("test.txt","wb")
	fw.write(data)
	fw.close()

if __name__ == "__main__":
	test()
